package niumao.minecraft.uhc.commands;

import org.bukkit.*;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.io.File;

public class Createworld implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (sender instanceof Player) {
            Player player = (Player) sender;
            if (!player.isOp()) {
                player.sendMessage("Only Op can execute this command!");
                return true;
            }
            if (cmd.getName().equalsIgnoreCase("createworld")) {
                if (args.length == 1) {
                    String str = args[0];
                    File file = new File(str);
                    if (str.equalsIgnoreCase("logs") || str.equalsIgnoreCase("plugins") || str.equalsIgnoreCase("backup")) {
                        sender.sendMessage("cannot create this world!");
                        return true;
                    }
                    if (file.exists() && file.isDirectory()) {
                        sender.sendMessage("this world are already existed!");
                        return true;
                    }
                    WorldCreator worldCreator = new WorldCreator(str);
                    sender.sendMessage("Creating World ...");
                    World world = Bukkit.createWorld(worldCreator);
                    assert world != null;

                    //uhc世界的基本设定
                    WorldBorder worldBorder = world.getWorldBorder();
                    worldBorder.setCenter(0, 0);
                    worldBorder.setSize(2000);
                    worldBorder.setDamageAmount(0.5);
                    worldBorder.setDamageBuffer(3);
                    world.setGameRule(GameRule.NATURAL_REGENERATION, false);
                    world.setGameRule(GameRule.SPECTATORS_GENERATE_CHUNKS, false);

                    sender.sendMessage("World has been created!");
                    return true;
                }
                return false;
            }
        }
        sender.sendMessage(ChatColor.RED + "only Player can executor this command!");
        return true;
    }
}
